package com.neusoft.bizcore.notice.notification.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.notice.constant.NoticeConsts;
import com.neusoft.bizcore.notice.notification.dto.NoticeRecordDTO;
import com.neusoft.bizcore.notice.notification.factory.MessageFactory;
import com.neusoft.bizcore.notice.notification.factory.impl.MailMessageFactory;
import com.neusoft.bizcore.notice.notification.factory.impl.NoticeMessageFactory;
import com.neusoft.bizcore.notice.notification.factory.impl.WechatMessageFactory;
import com.neusoft.bizcore.notice.notification.service.NoticeRecordService;
import com.neusoft.bizcore.notice.notification.service.NoticeService;
import com.neusoft.bizcore.web.exception.UnitedException;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeRecordService noticeRecordService;
    @Autowired
    private MailMessageFactory mailFactory;
    @Autowired
    private WechatMessageFactory wechatFactory;
    @Autowired
    private NoticeMessageFactory noticeSystemFactory;

    @Override
    public void buildAndSendMessage(final NoticeRecordDTO msg) {

        MessageFactory factory = null;
        if ((msg.getType() == null) || msg.getType().isEmpty()) {
            throw new UnitedException("消息类型");
        }
        final List<String> types = msg.getType();

        for (final String str : types) {
            if (str.trim().equals(NoticeConsts.INFORM_TYPE_MAIL)) {
                factory = this.mailFactory;
            } else if (str.trim().equals(NoticeConsts.INFORM_TYPE_WECHAT)) {
                factory = this.wechatFactory;
            } else if (str.trim().equals(NoticeConsts.INFORM_TYPE_MESSAGE)) {
                factory = null;
            } else if (str.trim().equals(NoticeConsts.INFORM_TYPE_PHONE)) {
                factory = null;
            } else if (str.trim().equals(NoticeConsts.INFORM_TYPE_NOTICE)) {
                factory = this.noticeSystemFactory;
            }
            if (factory != null) {
                factory.sendMessage(msg);
            }
        }

        //保存发送记录
        this.noticeRecordService.create(msg);
    }

}
